package com.ele.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ele.pojo.Shop;
import com.ele.utils.JDBCUtils;

public class ShopDao {

	public Shop shopLogin(int id, String password) throws SQLException {
		
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from shop where id = ? and shoppwd = ?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setInt(1, id);
		ptmt.setString(2, password);
		ResultSet rs = ptmt.executeQuery();
		if (rs.next()) {
			Shop shop = new Shop();
			shop.setId(rs.getInt("id"));
			shop.setShopName(rs.getNString("shopName"));
			shop.setPassword(rs.getString("shoppwd"));
			shop.setAddr(rs.getString("shopAddr"));
			shop.setShopDesc(rs.getString("shopDesc"));
			shop.setStartPrice(rs.getDouble("startPrice"));
			shop.setFee(rs.getDouble("fee"));
			shop.setShopStatus(rs.getInt("shopstatus"));
			JDBCUtils.close(conn, ptmt, rs);
			return shop;
		}
		return null;
	}

	public int shopLogin(Shop shop) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "update shop set shopName=?,"
				+ "shopAddr=?,shopdesc=?,startPrice=?,"
				+ "fee=?,shopStatus=?,shoppwd=? where id =?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, shop.getShopName());
		ptmt.setString(2, shop.getAddr());
		ptmt.setString(3, shop.getShopDesc());
		ptmt.setDouble(4, shop.getStartPrice());
		ptmt.setDouble(5, shop.getFee());
		ptmt.setInt(6, shop.getShopStatus());
		ptmt.setString(7, shop.getPassword());
		ptmt.setInt(8, shop.getId());
		
		return ptmt.executeUpdate();
		
	}

}
